﻿using FrameServer.Net;
using System;

namespace FrameServer.Handler
{
    class CommonHandler
    {
        public CommonHandler()
        {
            NetMsgHandler.Instance.RegisterReq(NetMsgID.CsLogin, HandleLogin);
            NetMsgHandler.Instance.RegisterReq(NetMsgID.CsHeartBeat, HandleHeartBeat);
        }

        private void HandleLogin(byte[] data, TCPClientPeer clientPeer)
        {
            CS_Login login = NetPacket.Deserialize<CS_Login>(data);
            var token = login.Token;
            // 从数据库加载详细信息
            clientPeer.token = token;
            if (TCPServer.Instance.clientPeerMap.ContainsKey(token))
            {
                clientPeer.SendTcpMsg(NetMsgID.ScLogin, new SC_Login { Result = NetRetCode.Error });
            }
            else
            {
                TCPServer.Instance.clientPeerMap[token] = clientPeer;
                clientPeer.SendTcpMsg(NetMsgID.ScLogin, new SC_Login { Result = NetRetCode.Success });
            }
        }

        private void HandleHeartBeat(byte[] data, TCPClientPeer clientPeer)
        {
            clientPeer.heartBeatFlag = true;
        }
    }
}
